﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace A_Modules.CheckBox_Header_Column_For_DataGridView
{
    public partial class TestDemo_CheckBoxDatagrid : Form
    {
        public TestDemo_CheckBoxDatagrid()
        {
            InitializeComponent();


            DataGridViewCheckBoxHeaderCell _checkBoxHeaderCell = new DataGridViewCheckBoxHeaderCell();


            

            this.dataGridView1.Columns[0].HeaderCell = _checkBoxHeaderCell;

            this.dataGridView1.Columns[0].HeaderText = "";

            _checkBoxHeaderCell.OnCheckBoxClicked += new CheckBoxClickedHandler(this.cbHeader_OnCheckBoxClicked);
        }

        public void cbHeader_OnCheckBoxClicked(bool _checked)
        {

            DataGridViewCheckBoxCell cbc;
            foreach (DataGridViewRow vRow in this.dataGridView1.Rows)
            {
                cbc =
                    vRow.Cells[0] as DataGridViewCheckBoxCell;

                if(cbc.Selected)
                {
                    cbc.Value = _checked;
                    cbc.EditingCellFormattedValue = _checked; //如果不赋值，被选中的行，不会跟着变//in edit mode

                    System.Console.WriteLine(cbc.Value+":"+cbc.EditingCellFormattedValue);
                }

            }
        }
    }
}
